package com.athui.bean.config;

import com.athui.enums.DriverEnums;
import com.athui.enums.JdbcTemplateEnums;
import org.apache.flink.shaded.guava30.com.google.common.base.Strings;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * @description: TODO
 * @autor: zhangzhonghui
 * @create: 2024-08-02 13:38
 * @Version: 1.0
 */
public class ConnectInfo implements Serializable {

    /**
     * @description: TODO 数据库驱动
     * @author: 阿辉大人
     * @create: 2024/8/2 21:50
     * @version: 1.0
    **/
    @NotNull
    private DriverEnums driver;
    /**
     * @description: TODO 连接 数据库的 IP
     * @author: 阿辉大人
     * @create: 2024/8/2 21:50
     * @version: 1.0
    **/
    private String host;
    /**
     * @description: TODO 连接数据库的 端口
     * @author: 阿辉大人
     * @create: 2024/8/2 21:50
     * @version: 1.0
    **/
    private Integer port;
    /**
     * @description: TODO 当 jdbc 不为空时， host 和 host 不会其作用
     * @author: 阿辉大人
     * @create: 2024/8/2 21:51
     * @version: 1.0
    **/
    private String jdbc;
    /**
     * @description: TODO 数据库名称
     * @author: 阿辉大人
     * @create: 2024/8/2 21:52
     * @version: 1.0
    **/
    @NotBlank(message = "database/schema is required")
    private String database;
    /**
     * @description: TODO 账号
     * @author: 阿辉大人
     * @create: 2024/8/2 21:52
     * @version: 1.0
    **/
    @NotBlank(message = "username is required")
    private String username;
    /**
     * @description: TODO 密码
     * @author: 阿辉大人
     * @create: 2024/8/2 21:52
     * @version: 1.0
    **/
    @NotBlank(message = "password is required")
    private String password;

    public ConnectInfo() {}

    public ConnectInfo(
            @NotNull DriverEnums driver,
            String host, Integer port,
            @NotBlank(message = "database/schema is required") String database,
            @NotBlank(message = "username is required") String username,
            @NotBlank(message = "password is required") String password
    ) {
        this.driver = driver;
        this.host = host;
        this.port = port;
        this.database = database;
        this.username = username;
        this.password = password;
    }

    public ConnectInfo(
            @NotNull DriverEnums driver,
            String jdbc,
            @NotBlank(message = "database/schema is required") String database,
            @NotBlank(message = "username is required") String username,
            @NotBlank(message = "password is required") String password
    ) {
        this.driver = driver;
        this.jdbc = jdbc;
        this.database = database;
        this.username = username;
        this.password = password;
    }



    public DriverEnums getDriver() {
        return driver;
    }

    public void setDriver(DriverEnums driver) {
        this.driver = driver;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getJdbc() {
        if (Strings.isNullOrEmpty(jdbc)) {
            if(this.driver.equals(DriverEnums.MYSQL5_DRIVER) || this.driver.equals(DriverEnums.MYSQL8_DRIVER)){
                // 配置 mysql jdbc url
                jdbc = String.format(JdbcTemplateEnums.MYSQL.toString(), this.host, this.port, this.database);
            }else if(this.driver.equals(DriverEnums.ORACLE_DRIVER)){
                jdbc = String.format(JdbcTemplateEnums.ORACLE.toString(), this.host, this.port, this.database);
            }else if(this.driver.equals(DriverEnums.SQLSERVER_DRIVER)){
                jdbc = String.format(JdbcTemplateEnums.SQLSERVER.toString(), this.host, this.port, this.database);
            }else if(this.driver.equals(DriverEnums.HIVE_DRIVER)) {
                jdbc = String.format(JdbcTemplateEnums.HIVE.toString(), this.host, this.port, this.database);
            }
        }
        return jdbc;
    }

    public void setJdbc(String jdbc) {
        this.jdbc = jdbc;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "ConnectInfo{" +
                "driver=" + driver +
                ", host='" + host + '\'' +
                ", port=" + port +
                ", jdbc='" + jdbc + '\'' +
                ", database='" + database + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
